package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import model.Card;
import model.CardType;
import model.Player;
import model.TradeCard;

public class TradeCardsFrame extends JFrame{

	
	private static final long serialVersionUID = -8596282370327238701L;
	private Player player;
	private int cardsSelected;
	private List<Card> cards;
	private List<Card> selectedCards;
	private boolean[] selected;
	private JPanel gridCards;
	
	/**
	 * Stand Alone Frame that allows Player to Trade in cards
	 *
	 * @param p
	 */
	public TradeCardsFrame(Player p){
		player = p;
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setTitle("Trade In Your Cards Here");
		this.setMinimumSize(new Dimension(650, 370));
		this.setLayout(new BorderLayout());
		
		// temp, make sure player has at least one card
		//player.receiveCard(new Card(CardType.CANNON, null));
//		player.receiveCard(new Card(CardType.SOLDIER, null));
//		player.receiveCard(new Card(CardType.HORSE, null));
//		player.receiveCard(new Card(CardType.WILD, null));
		
		
		
		cards = player.getCards();
		selectedCards = new ArrayList<Card>();
		selected = new boolean[cards.size()];
		
		GridLayout picLayout = new GridLayout(1,0);
		picLayout.setHgap(5);
		
		JPanel gridCards = new JPanel();
		gridCards.setLayout(picLayout);
		
		int index = 0;
		Iterator<Card> it  = cards.iterator();
		while (it.hasNext()){
			Card card = it.next();
			
			ImageIcon image = new ImageIcon("images/cards/"+card.getType().toString()+".png");
			JLabel imageLabel = new JLabel();
			imageLabel.setIcon(image);			
			JLabel cardLabel;
			if(card.getType() != CardType.WILD){
				cardLabel = new JLabel(card.getTerritory().getTerritoryName());
				//cardLabel = new JLabel("TEMP LABEL");
			}
			else{
				cardLabel = new JLabel("WILD");
			}
			
			JPanel cardPanel = new JPanel();
			cardPanel.setLayout(new BorderLayout());
			cardPanel.add(imageLabel, BorderLayout.CENTER);
			cardPanel.add(cardLabel, BorderLayout.NORTH);
			
			JCheckBox cardBox = new JCheckBox();
			cardBox.addItemListener(new CardSelectListener(card, index));
			
			cardPanel.add(cardBox, BorderLayout.SOUTH);
			
			gridCards.add(cardPanel);
			
			index ++;
		}
			
		JScrollPane scrollP = new JScrollPane(gridCards);
		scrollP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollP.setMaximumSize(new Dimension(450,200));
		scrollP.setPreferredSize(new Dimension(450,200));
		
		this.add(scrollP, BorderLayout.CENTER);		
		//JLabel temp = new JLabel("TRADE IN YOUR CARDS!!!!!!!!!!!!");
		JButton tradeButton = new JButton("Trade Selected Cards");
		tradeButton.addActionListener(new TradeCardsListener());
		//this.add(temp, BorderLayout.NORTH);
		this.add(tradeButton, BorderLayout.SOUTH);
		
		this.setVisible(true);
	}
	
	private class CardSelectListener implements ItemListener {

		private Card card;
		private int index;
		
		public CardSelectListener(Card c, int i){
			card =  c;
			index = i;
		}
		@Override
		public void itemStateChanged(ItemEvent arg0) {
			if( arg0.getStateChange() == ItemEvent.DESELECTED){
				if(selectedCards.contains(card)){
					selected[index] = false;
					selectedCards.remove(card);
				}
			}
			else{
				if(!selectedCards.contains(card) ){
					selected[index] = true;
					selectedCards.add(card);
				}
			}
			System.out.println("Selected[+"+index+"] "+selected[index]);
		}
	}
	
	private class TradeCardsListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			boolean success = false;
			int beforeTrade = player.getBonus();
			int afterTrade = 0;
			if(selectedCards.size() == 3 && selectedCards.get(0).getTerritory() != null){
				beforeTrade = player.getBonus();
				success = TradeCard.tradeCard(player, selectedCards.get(0), selectedCards.get(1), selectedCards.get(2));
				afterTrade = player.getBonus();
			}
			if(success){
				JOptionPane.showMessageDialog(null,
				"Cards Traded!!! You Gained "+(afterTrade-beforeTrade)+" Troops");
				setVisible(false);
				dispose();
			}
			else {
				JOptionPane.showMessageDialog(null,
				"FAIL! your doing it wrong NOOB");
			}
		}
		
		
		
	}
	
	
}
